package com.xzy.common.util;

import java.util.ArrayList;
import java.util.List;

/**
 * 字符串工具类
 *
 * @author xzy.xiao
 * @date 2022/8/18  11:08
 */
public class StringUtil {

    public static final int MOVE_BACK = 0;
    public static final int MOVE_FORWARD = 1;

    /**
     * 移动切分字符串
     *
     * @param str             待切分的字符串
     * @param subLength       切分长度
     * @param movingDirection 移动方向（0：后移 1：前移）
     * @return 切分好的所有子串
     */
    public static List<String> moveAndSub(String str, int subLength, int movingDirection) {

        List<String> subStrList = new ArrayList<>();

        int strLength = str.length();
        if (strLength <= subLength) {
            subStrList.add(str);
            return subStrList;
        }

        if (movingDirection == MOVE_BACK) {
            for (int i = 0; i <= strLength - subLength; i++) {
                subStrList.add(str.substring(i, i + subLength));
            }
        } else {
            for (int i = strLength; i >= subLength; i--) {
                subStrList.add(str.substring(i - subLength, i));
            }
        }

        return subStrList;
    }

    public static List<String> moveBackAndSub(String str, int subLength) {
        return moveAndSub(str, subLength, MOVE_BACK);
    }

    public static List<String> moveForwardAndSub(String str, int subLength) {
        return moveAndSub(str, subLength, MOVE_FORWARD);
    }
}
